package com.huanke.iot.api.controller.app;

import com.google.common.collect.Lists;
import com.huanke.iot.api.controller.app.response.AppDeviceDataVo;
import com.huanke.iot.api.controller.app.response.AppDeviceListVo;
import com.huanke.iot.api.controller.app.response.AppInfoVo;
import com.huanke.iot.api.controller.app.response.AppSceneVo;
import com.huanke.iot.api.controller.h5.BaseController;
import com.huanke.iot.api.controller.h5.req.*;
import com.huanke.iot.api.controller.h5.response.DeviceModelVo;
import com.huanke.iot.api.controller.h5.response.LocationVo;
import com.huanke.iot.api.controller.h5.response.SensorDataVo;
import com.huanke.iot.api.controller.h5.response.WeatherVo;
import com.huanke.iot.api.service.device.basic.AppBasicService;
import com.huanke.iot.api.service.device.basic.AppDeviceDataService;
import com.huanke.iot.api.service.device.basic.DeviceDataService;
import com.huanke.iot.api.service.device.basic.DeviceService;
import com.huanke.iot.base.api.ApiResponse;
import com.huanke.iot.base.cache.Cache;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@RequestMapping("/app/api/base")
@Slf4j
@RestController
public class AppController extends BaseController {

    @Autowired
    private DeviceDataService deviceDataService;

    @Autowired
    private AppBasicService appBasicService;

    @Autowired
    private AppDeviceDataService appDeviceDataService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Autowired
    private DeviceService deviceService;

    @Value("${apkKey}")
    private String apkKey;

    @GetMapping("/removeIMeiInfo")
    public ApiResponse<Object> removeIMeiInfo(HttpServletRequest request) {
        return appBasicService.removeIMeiInfo(request);
    }

    @GetMapping("/setApkInfo")
    public void setApkInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String objectApiResponse = appBasicService.addUserAppInfo(request);
        response.setContentType(ContentType.TEXT_HTML.getMimeType());
        response.setCharacterEncoding("UTF-8");
        response.getWriter().print("<span style='font-size:55px'>"+objectApiResponse+"</span>");
    }

    @PostMapping("/getQRCode")
    public ApiResponse<Object> getQRCode(HttpServletRequest request) {
        String appId = request.getParameter("appId");
        return new ApiResponse<>(appBasicService.getQRCode(appId));
    }

    @GetMapping("/queryDeviceList")
    public ApiResponse<AppDeviceListVo> queryDeviceList() {
        Integer userId = getCurrentUserIdForApp();
        log.debug("查询我的设备列表，userId={}", userId);
        try {
            AppDeviceListVo deviceListVo = appDeviceDataService.obtainMyDevice(userId);
            return new ApiResponse<>(deviceListVo);
        }catch (Exception e){
            log.error("queryData error {}",userId ,e);
        }
        return   new ApiResponse(Lists.newArrayList());
    }

    @PostMapping("/queryChildDevice/{hostDeviceId}")
    public Object obtainChildDevice(@PathVariable("hostDeviceId") Integer hostDeviceId) {
        Integer userId = getCurrentUserIdForApp();
        Integer customerId = getCurrentCustomerId();
        log.info("查询子设备列表，userId={}， hostDeviceId={}", userId, hostDeviceId);
        List<AppDeviceListVo.DeviceItemPo> childDeviceList = appDeviceDataService.queryChildDevice(hostDeviceId,customerId);
        return new ApiResponse<>(childDeviceList);
    }

    @PostMapping("/getModelVo")
    public ApiResponse<DeviceModelVo> getModelVo(@RequestBody DeviceFormatRequest request) {
        Integer deviceId = request.getDeviceId();
        log.debug("获取功能项，deviceId={}", deviceId);
        DeviceModelVo deviceModelVo = appBasicService.getModelVo(deviceId);
        return new ApiResponse<>(deviceModelVo);
    }

    @PostMapping("/queryDetailByDeviceId")
    public ApiResponse<List<AppDeviceDataVo>> queryDetailByDeviceId(@RequestBody DeviceAbilitysRequest request) {
        Integer deviceId = request.getDeviceId();
        log.debug("查询设备详情，设备ID={}",deviceId);
        List<Integer> abilityIds = request.getAbilityIds();
        List<AppDeviceDataVo> deviceAbilityVos = appDeviceDataService.queryDetailAbilitysValue(deviceId,abilityIds);
        return new ApiResponse<>(deviceAbilityVos);
    }

    @GetMapping("/getWeatherAndLocation/{deviceId}")
    public ApiResponse<List<Object>> queryDeviceWeather(@PathVariable("deviceId") String deviceId) {
        log.debug("查询设备天气，设备ID={}",deviceId);
        if(StringUtils.isEmpty(deviceId) || "null".equalsIgnoreCase(deviceId)){
            return ApiResponse.PARAM_ERROR;
        }

        WeatherVo weatherVo = deviceService.queryDeviceWeather(Integer.valueOf(deviceId));
        LocationVo locationVo = deviceService.queryDeviceLocation(Integer.valueOf(deviceId));
        if (StringUtils.isNotEmpty(locationVo.getArea())){
            String[] s = locationVo.getArea().split(" ");
            if(s.length>3) {
                locationVo.setArea(StringUtils.isEmpty(s[1])?s[2]:s[1]);
            }
        }
        List resp = new ArrayList();
        resp.add(weatherVo);
        resp.add(locationVo);
        return new ApiResponse<>(resp);
    }

    @PostMapping("/getHistoryData")
    public ApiResponse<List<SensorDataVo>> getHistoryData(@RequestBody HistoryDataRequest request) {
        Integer deviceId = request.getDeviceId();
        Integer type = request.getType();
        Integer userId = getCurrentUserIdForApp();

        //缓存对象名称
        String cacheName = "getHistoryData_"+deviceId+"_"+type+"_"+userId;
        //从缓存中获取对象
        List<SensorDataVo> list = (List<SensorDataVo>) Cache.get(cacheName);
        //如果缓存中没有，则从数据库获取数据,并将获取的结果存入缓存中
        if (list == null) {
            list = deviceDataService.getHistoryData(deviceId, type);
            Cache.put(cacheName, list, Cache.CACHE_HOLD_TIME_5M);//加入缓存，有效期为24小时
        }
        log.debug("查询设备历史曲线：userId={}, deviceId={}, type={}", userId, deviceId, type);
        return new ApiResponse<>(list);
    }

    @PostMapping("/editDevice")
    public ApiResponse<Boolean> editDevice(@RequestBody DeviceRequest request) {
        Integer deviceId = request.getDeviceId();
        String deviceName = request.getDeviceName();
        Integer userId = getCurrentUserIdForApp();
        boolean ret = deviceService.editDevice(userId, deviceId, deviceName);
        return new ApiResponse<>(ret);
    }

    @GetMapping("/obtainApk")
    public ApiResponse<AppInfoVo> obtainApk(HttpServletRequest request) {
//        String apkInfo = stringRedisTemplate.opsForValue().get(apkKey);
//        if (StringUtils.isNotEmpty(apkInfo)) {
//            AppInfoVo appInfoVo = JSON.parseObject(apkInfo, AppInfoVo.class);
//            return new ApiResponse<>(appInfoVo);
//        }
//        return new ApiResponse<>();
        String appId = request.getParameter("appId");
        String appNo = request.getParameter("appNo");
        return new ApiResponse<>(appBasicService.getApkInfo(appId,appNo));
    }

    @PostMapping("/sendFunc")
    public ApiResponse<Boolean> sendFuc(@RequestBody DeviceFuncVo deviceFuncVo){
        log.debug("发送指令："+deviceFuncVo.toString());
        String funcId = deviceFuncVo.getFuncId();
        Boolean request = deviceDataService.sendFuncs(deviceFuncVo,getCurrentUserIdForApp(),2);
        return new ApiResponse<>(request);
    }

    @PostMapping("/getAppPassword")
    public ApiResponse<String> getAppPassword(HttpServletRequest request){
        String appId = request.getParameter("appId");
        log.debug("获取设备选择密码，appId={}",appId);
        String response= appBasicService.getPassword(appId);
        return new ApiResponse<>(response);
    }

    @PostMapping("/getCustomerScene")
    public ApiResponse<AppSceneVo> getCustomerScene(){
        AppSceneVo request= appBasicService.getCustomerSceneInfo();
        return new ApiResponse<>(request);
    }


}
